//
//  LWPersonageViewController.m
//  LWShopItem
//
//  Created by mac on 17/3/20.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWPersonageViewController.h"
#import "LWHeaderView.h"
#import "UIColor+Addition.h"
#import "LWGroupItem.h"
#import "LWSettingItem.h"
#import "LWOrderViewController.h"
#import "LWAddressViewController.h"
#import "LWDiscountTabVc.h"
#import "LWAddLocationVc.h"
#import "LWServiceViewController.h"


@interface LWPersonageViewController ()

//当前界面的数据源 用来存储分组模型
@property (nonatomic ,strong) NSMutableArray *groupArray;

@property (nonatomic ,strong) LWHeaderView *headerView;

@end

@implementation LWPersonageViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated {
    
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"token"]) {
        _headerView = (LWHeaderView *)self.tableView.tableHeaderView;
        [_headerView.logInBtn setTitle:[[NSUserDefaults standardUserDefaults] valueForKey:@"nickName"] forState:UIControlStateNormal];
        _headerView.logInBtn.enabled = NO;
    } else {
        [_headerView.logInBtn setTitle:@"请登录" forState:UIControlStateNormal];
        _headerView.logInBtn.enabled = YES;
    }
}

//groupArray懒加载
- (NSMutableArray *)groupArray
{
    if (!_groupArray) {
        _groupArray = [NSMutableArray array];
    }
    return _groupArray;
}

- (instancetype)init
{
    // 设置tableView的分组样式为Group样式
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    //添加第一组模型
    [self group1];
    [self group2];
    [self group4];
}

- (void)group1
{
    //创建组模型
    LWGroupItem *group0 = [[LWGroupItem alloc] init];
    
    //创建行模型 图片我就随便用TabBar的图片代替
    LWSettingItem *item = [LWSettingItem itemWithTitle:@"我的订单" icon:@"1"];
    
    //保存模型数组
    group0.itemsArray = @[item];
    
    //把组模型保存到groupArray数组中
    [self.groupArray addObject:group0];
    
}


- (void)group2
{
    //创建组模型
    LWGroupItem *group1 = [[LWGroupItem alloc] init];
    
    //创建行模型
    LWSettingItem *item = [LWSettingItem itemWithTitle:@"收货地址" icon:@"7"];
    LWSettingItem *item1 = [LWSettingItem itemWithTitle:@"我的优惠券" icon:@"3"];
   
   
   
    //保存模型数组
    group1.itemsArray = @[item,item1];
    
    //把组模型保存到groupArray数组中
    [self.groupArray addObject:group1];
}

- (void)group4
{
    //创建组模型
    LWGroupItem *group4 = [[LWGroupItem alloc] init];
    
    //创建行模型 图片我就随便用TabBar的图片代替
    LWSettingItem *item1 = [LWSettingItem itemWithTitle:@"客服中心" icon:@"8"];
    LWSettingItem *item = [LWSettingItem itemWithTitle:@"注销登录" icon:@"8"];
    
    //保存模型数组
    group4.itemsArray = @[item1,item];
    
    //把组模型保存到groupArray数组中
    [self.groupArray addObject:group4];
    
}



#pragma MARK - UI
- (void)setupUI
{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHex:0x9ACD32];
    
    LWHeaderView *headerV = [[LWHeaderView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 160)];
    
    headerV.backgroundColor = [UIColor orangeColor];
    
    
    
    [self.view addSubview:headerV];
    
    //包装
    self.tableView.tableHeaderView = headerV;

}

/**
 *  返回有多少组的代理方法
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.groupArray.count;
}

/**
 *  返回每组有多少行的代理方法
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    LWGroupItem *group = self.groupArray[section];
    return group.itemsArray.count;
}

/**
 *  返回每一行Cell的代理方法
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 设置Cell的重用标识
    static NSString *ID = @"cell";
    
    // 去缓存池中取Cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 若取不到便创建一个带重用标识的Cell
    if (cell == nil) {
        cell  = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    // 设置Cell右边的小箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //  取出组模型
    LWGroupItem *group = self.groupArray[indexPath.section];
    
    //  根据组模型取出行（Cell）模型
    LWSettingItem *item = group.itemsArray[indexPath.row];
    
    //  根据行模型的数据赋值
    cell.textLabel.text = item.title;
    
    // icon
    cell.imageView.image = [UIImage imageNamed:item.icon];
    return cell;
}

#pragma mark -tableView的代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        //不能写0，写0不起作用。
        //0代表默认的组头高度。
        return 0.1;
    }else{
        return 16;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    if (indexPath.section == 0) {
        
        {
            LWOrderViewController *orderVc = [LWOrderViewController new];
            
            //push 之前隐藏底部tabbar
            self.hidesBottomBarWhenPushed=YES;
            
            [self.navigationController pushViewController:orderVc animated:YES];
            
            //push 之后显示tabbar
            self.hidesBottomBarWhenPushed=NO;
            
            
        }

        
        
    }else if (indexPath.section == 1)
        
    {
        switch (indexPath.row) {
            case 0:
                NSLog(@"点击了订单");
            {
                LWAddLocationVc *ADvc = [LWAddLocationVc new];
                
                
                //push 之前隐藏底部tabbar
                self.hidesBottomBarWhenPushed=YES;
                
                [self.navigationController pushViewController:ADvc animated:YES];
                
                //push 之后显示tabbar
                self.hidesBottomBarWhenPushed=NO;
                
                
            }
                
            break;
                
            case 1:
                NSLog(@"点击了优惠券");
            {
                LWDiscountTabVc *disCount = [LWDiscountTabVc new];
                
                
                //push 之前隐藏底部tabbar
                self.hidesBottomBarWhenPushed=YES;
                
                [self.navigationController pushViewController:disCount animated:YES];
                
                //push 之后显示tabbar
                self.hidesBottomBarWhenPushed=NO;

                
            
        }
                break;
                
            default:
                break;
        }
        
    }else if (indexPath.section == 2)
    {
        switch (indexPath.row) {
            case 0:
                NSLog(@"点击了客服中心");
            {
                LWServiceViewController *vc = [LWServiceViewController new];
                
                //push 之前隐藏底部tabbar
                self.hidesBottomBarWhenPushed=YES;
                
                [self.navigationController pushViewController:vc animated:YES];
                
                //push 之后显示tabbar
                self.hidesBottomBarWhenPushed=NO;

                
            }
                
                break;
            case 1:
                NSLog(@"点击了退出");
            {
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                
                [userDefaults removeObjectForKey:@"token"];
                [userDefaults removeObjectForKey:@"nickName"];
                
                //nickname
                [userDefaults removeObjectForKey:@"nickname"];
                [userDefaults removeObjectForKey:@"headimgurl"];
                
                
                [userDefaults removeObjectForKey:@"user_id"];
                
                [userDefaults synchronize];
                
                
                [_headerView.logInBtn setTitle:@"请登录" forState:UIControlStateNormal];
                
                _headerView.logInBtn.enabled = YES;
                
            }

                break;
                
            default:
                break;
        }
    }
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    
}
@end

